package com.hex.liyue.commonmodule.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.hex.liyue.R;


/**
 * Created by yukuo on 2016/4/7.
 * 这是一个自定义的一个快速索引的自定义的控件
 */
public class FastIndexBar extends View {
    /**
     * 上下文
     */
    private Context context;
    /**
     * 宽度
     */
    private int Width;
    /**
     * 高度
     */
    private int Height;
    /**
     * 字体的颜色
     */
    private int TextColor;
    /**
     * 字体大小
     */
    private int TextSize;
    /**
     * 控件的背景色
     */
    private int BackgroundColor;

    /**
     * 单元格高度
     */
    private float cellHeight;
    private static final String[] WORDS = new String[]{"搜", "#", "A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //这是一个画笔
    private Paint paint;
    //这是一个索引
    int index = -1;
    //监听器
    private OnLetterChangeListener onLetterChangeListener;

    /**
     * 字母更新监听
     */
    public interface OnLetterChangeListener {
        /**
         * 当手指移动或者按下的时候调用的方法
         *
         * @param letter
         */
        void onLetterChange(String letter);

        /**
         * 当手指离开这个控件的时候调用的方法
         */
        void onUp();
    }

    public void setOnLetterChangeListener(
            OnLetterChangeListener onLetterChangeListener) {
        this.onLetterChangeListener = onLetterChangeListener;
    }

    public FastIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public FastIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs, defStyleAttr);
    }

    /**
     * 初始化各种属性
     */
    private void init(AttributeSet attrs, int defStyleAttr) {
        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FastIndexBar, defStyleAttr, 0);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.FastIndexBar_BackgroundColor) {
                BackgroundColor = typedArray.getColor(attr, Color.WHITE);
            } else if (attr == R.styleable.FastIndexBar_TextColor) {
                TextColor = typedArray.getColor(attr, Color.BLUE);
            } else if (attr == R.styleable.FastIndexBar_TextSize) {
                TextSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));
            }
        }
        //释放资源
        typedArray.recycle();
        /**
         * 初始化一个画笔
         */
        paint = new Paint();
        paint.setColor(TextColor);//设置画笔的颜色
        paint.setTextSize(TextSize);//设置字体的大小
    }

    /**
     * 绘制
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < WORDS.length; i++) {
            String text = WORDS[i];
            float x = (int) (Width * 0.5f - paint.measureText(text) * 0.5f);
            Rect bounds = new Rect();
            // 把矩形对象赋值
            paint.getTextBounds(text, 0, text.length(), bounds);
            int textHeight = bounds.height();
            float y = (int) (cellHeight * 0.5f + textHeight * 0.5 + cellHeight
                    * i);
            // 如果当前绘制的字母和按下的字母索引一样, 用灰色的画笔
            //  paint.setColor(i == index ? Color.GRAY : Color.BLACK);
            canvas.drawColor(BackgroundColor);
            canvas.drawText(text, x, y, paint);
        }
    }

    /**
     * 控件的手指触摸事件的监听
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = -1;
        //选择的索引
        int currentIndex = -1;
        switch (MotionEventCompat.getActionMasked(event)) {
            case MotionEvent.ACTION_DOWN://按下的时候
                y = event.getY();//获得当时按下的y值
                currentIndex = (int) (y / cellHeight);
                if (currentIndex >= 0 && currentIndex < WORDS.length) {
                    // 健壮性处理, 在正常范围内
                    if (index != currentIndex) {
                        // 字母的索引发生了变化
                        if (onLetterChangeListener != null) {
                            onLetterChangeListener
                                    .onLetterChange(WORDS[currentIndex]);
                        }
                        index = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE://移动的时候
                y = event.getY();
                currentIndex = (int) (y / cellHeight);
                if (currentIndex >= 0 && currentIndex < WORDS.length) {
                    // 健壮性处理, 在正常范围内
                    if (index != currentIndex) {
                        // 字母的索引发生了变化
                        if (onLetterChangeListener != null) {
                            onLetterChangeListener
                                    .onLetterChange(WORDS[currentIndex]);
                        }
                        index = currentIndex;
                    }
                }
                break;
            case MotionEvent.ACTION_UP://离开的时候
                index = -1;
                if (onLetterChangeListener != null) {
                    onLetterChangeListener
                            .onUp();
                }
                break;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //单元格宽度
        Width = getMeasuredWidth();
        Height = getMeasuredHeight();
        //单元格高度
        cellHeight = Height * 1.0f / WORDS.length;
    }
}
